#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <climits>
using namespace std;

int main() 
{
    int num = 0, dis = INT_MAX;
    cin >> num;

    string str1, str2;
    cin >> str1 >> str2;

    vector<pair<string,int>> vs;
    for(int i=0; i<num; i++)
    {
        string tmp;
        cin >> tmp;
        if(tmp == str1 || tmp == str2) vs.emplace_back(tmp,i);
    }

    if(str1 == "" || str2 == "" || vs.size() == 0 || vs.size() == 1) 
    {
        cout << -1 << endl;
        return 0;
    }
    
    for(int i=0; i<vs.size()-1; i++)
    {
        if(vs[i].first != vs[i+1].first)
        {
            dis = min(abs(vs[i].second-vs[i+1].second),dis);
        }
    }

    if(dis == INT_MAX) cout << -1;
    else cout << dis;

    return 0;
}
// 64 位输出请用 printf("%lld")